package org.bread.worm.cms.channel.dao.impl;

import java.util.List;

import org.bread.worm.cms.basic.dao.impl.BaseDaoImpl;
import org.bread.worm.cms.channel.bean.Channel;
import org.bread.worm.cms.channel.bean.ChannelTree;
import org.bread.worm.cms.channel.bean.ChannelType;
import org.bread.worm.cms.channel.dao.ChannelDao;
import org.springframework.stereotype.Repository;

@Repository("channelDao")
public class ChannelDaoImpl extends BaseDaoImpl<Channel> implements ChannelDao {
	public static void initTreeNode(List<ChannelTree> cts) {
		cts.add(0,new ChannelTree(Channel.ROOT_ID,Channel.ROOT_NAME,-1));
		for(ChannelTree ct:cts) {
			if(ct.getPid()==null)ct.setPid(0);
		}
	}

	@Override
	public List<Channel> listByParentId(Integer pid) {
		String sql = "select c from Channel c left join fetch c.parent cp where 1 = 1";
		if (pid == null || pid == 0) {
			sql += " and cp.id is null";
		}else{
			sql += " and cp.id = " + pid;
		}
		return listByHql(sql);
	}

	@Override
	public int getMaxOrderByParentId(Integer pid) {
		String sql = "select max(c.orders) from Channel c where 1 = 1";
		if (pid != null) {
			sql += " and c.parent.id = " + pid;
		}else{
			sql += " and c.parent.id is null";
		}
		Object object = queryObject(sql);
		if(object == null) return 0;
		return (Integer)object;
	}

	@Override
	public List<ChannelTree> generateTree() {
		String sql = "select id,name,pid from t_channel order by orders";
		return listBySql(sql, ChannelTree.class, false);
	}

	@Override
	public List<ChannelTree> generateTreeByParent(Integer pid) {
		String sql = "select id,name,pid from t_channel where 1 = 1";
		
		if (pid == null) {
			sql += " and pid is null";
		}else {
			sql += " and pid = " + pid;
		}
		
		sql += " order by orders";
		return listBySql(sql, ChannelTree.class, false);
	}

	@Override
	public void updateSort(Integer[] ids) {
		int index  = 0;
		String sql = "update c Channel set c.orders = ? where c.id = ?";
		for (Integer id : ids) {
			updateByHql(sql, new Object[]{index++,id});
		}
		
	}

	@Override
	public List<Channel> listPublishChannel() {
		String hql = "select new Channel(c.id,c.name) from Channel c where c.status=1 and c.type!="+ChannelType.NAV_CHANNEL.ordinal();
		return this.listByHql(hql);
	}

	@Override
	public List<Channel> listTopNavChannel() {
		String hql = "select new Channel(c.id,c.name,c.customLink,c.customLinkUrl) " +
				"from Channel c where c.status=0 and c.isTopNav=1 order by navOrder";
		return this.listByHql(hql);
	}

	@Override
	public List<Channel> listAllIndexChannel(ChannelType ct) {
		String hql = "select new Channel(c.id,c.name) " +
				"from Channel c where c.status=0 and c.isIndex=1";
		if(ct!=null) {
			hql+=" and c.type="+ct.ordinal();
		}
		return this.listByHql(hql);
	}

	@Override
	public void deleteChannelGroups(int cid) {
		String hql = "delete GroupChannel gc where gc.channel.id=?";
		this.getSession().createQuery(hql).setParameter(0, cid).executeUpdate();
	}

	@Override
	public Channel loadFirstChannelByNav(int pid) {
		String hql = "select new Channel(c.id,c.name,c.type) from Channel c where c.parent.id=? order by c.orders";
		return (Channel)this.getSession().createQuery(hql)
			.setFirstResult(0).setMaxResults(1).setParameter(0, pid).uniqueResult();
	}

	@Override
	public List<Channel> listUseChannelByParent(Integer pid) {
		String hql = "select c from Channel c left join fetch c.parent cp where cp.id="+pid+" and cp.status=0 order by c.orders";
		if(pid==null||pid==0) hql = "select c from Channel c where c.parent is null and cp.status=0 order by c.orders";
		return this.listByHql(hql);
	}

	@Override
	public List<Channel> listChannelByType(ChannelType ct) {
		String hql = "select new Channel(c.id,c.name) from Channel c where c.status=0 and c.type=?";
		return this.listByHqlSingle(hql,ct);
	}

}
